#include <sys/times.h>
#include <stdio.h>
#include <stdint.h>

/*系统的时间片大小，一般是10MS 或者 1MS*/
/*系统的时间片大小，一般是10MS 或者 1MS*/
#if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE
#define K_MS 1
#else
#define K_MS 10 	
#endif

#define APP_DELAY_LIMIT 0xff000000U

uint32_t app_get_systime(void)
{
    long t = times(0);
	return (uint32_t)t;
}

void app_sleep(volatile uint32_t *pt_cnt, uint32_t ums)
{
    if(ums < APP_DELAY_LIMIT) {
	    *pt_cnt= app_get_systime()+ ums/K_MS;
    }
}

uint8_t app_pend_wake(uint32_t pt_cnt)
{
	uint32_t off = app_get_systime()- pt_cnt;
	if( (off>0) && (off<APP_DELAY_LIMIT) ) {
		return 1;
	}
	else {
		return 0;
	}
}

uint8_t app_capture_interval(volatile uint32_t *pt_cnt, uint32_t ums)
{
    uint8_t ret = 0;
    if(ums < APP_DELAY_LIMIT) {
        uint32_t off = app_get_systime()- *pt_cnt;
        ums/=K_MS; 
	    if( (off > ums) && (off<APP_DELAY_LIMIT) ) {
		    ret = 1;
        }
    }    
    return ret;
}

void app_update_interval(volatile uint32_t *pt_cnt)
{
	*pt_cnt = app_get_systime();
}




